home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 19 / bix01.zip / INTEGERS.LIB < prev    next >
Text File  |  1986-07-07  |  4KB  |  156 lines

  1. {
  2.                  procedure and functions in this library
  3.  
  4.   Sign               returns sign (-1,0,+1) of integer value
  5.   Min                returns minimum of two integers
  6.   Max                returns maximum of two integers
  7.   ISwap              trade two integer values
  8.   ISqrt              returns integer square root of integer value
  9.   Condition          forces integer into the range Min..Max
  10.   AMin               returns minimum integer in array
  11.   AMax               returns maximum integer in array
  12.  
  13. }
  14.  
  15. function Sign(Val : Integer) : Integer;
  16. {
  17.        purpose       returns sign (-1,0,1) of Val
  18.        last update   23 Jun 85
  19. }
  20. begin
  21.   if Val > 0
  22.     then Sign := 1
  23.   else if Val < 0
  24.     then Sign := -1
  25.     else Sign :=  0
  26. end; { of func Sign }
  27.  
  28. function Min(Val1,Val2 : Integer) : Integer;
  29. {
  30.        purpose       returns minimum of two integers
  31.        last update   08 Jul 85
  32. }
  33. begin
  34.   if Val1 < Val2
  35.     then Min := Val1
  36.     else Min := Val2
  37. end; { of func Min }
  38.  
  39. function Max(Val1,Val2 : Integer) : Integer;
  40. {
  41.        purpose       returns maximum of two integers
  42.        last update   08 Jul 85
  43. }
  44. begin
  45.   if Val1 > Val2
  46.     then Max := Val1
  47.     else Max := Val2
  48. end; { of func Max }
  49.  
  50. procedure ISwap(var Val1,Val2 : Integer);
  51. {
  52.        purpose       swaps values of Val1 and Val2
  53.        last update   08 Jul 85
  54. }
  55. var
  56.   Temp               : Integer;
  57. begin
  58.   Temp := Val1;
  59.   Val1 := Val2;
  60.   Val2 := Temp
  61. end; { of proc ISwap }
  62.  
  63. function ISqrt(Val : Integer) : Integer;
  64. {
  65.        purpose       returns integer square root of Val
  66.        note well:    this routine rounds to the nearest square root
  67.        last update   23 Jan 85
  68. }
  69. var
  70.   OddSeq,Square,Root : Integer;
  71. begin
  72.   OddSeq := -1;
  73.   Square :=  0;
  74.   repeat
  75.     OddSeq := OddSeq + 2;
  76.     Square := Square + OddSeq
  77.   until Val < Square;
  78.   Root := Succ(OddSeq shr 1);
  79.   if Val <= Square - Root
  80.     then Root := Pred(Root);
  81.   ISqrt := Root
  82. end; { of func ISqrt }
  83.  
  84. procedure Condition(Min : Integer; var Val : Integer; Max : Integer);
  85. {
  86.        purpose       forces Min <= Val <= Max
  87.        last update   08 Jul 85
  88. }
  89. begin
  90.   if Max < Min
  91.     then ISwap(Min,Max);
  92.   if Val < Min
  93.     then Val := Min
  94.   else if Max < Val
  95.     then Val := Max
  96. end; { of proc Condition }
  97.  
  98. function AMin(var IAddr; Size : Integer; var Mndx : Integer) : Integer;
  99. {
  100.        purpose       finds minimum value in integer array
  101.  
  102.        note          Size should be the size in *words*; if the
  103.                      the function SizeOf is used, then you need
  104.                      to divide the result by 2 before passing it on:
  105.                      MVal := AMin(IArray,(SizeOf(IArray) shr 1),Indx);
  106.  
  107.        last update   09 Jul 85
  108. }
  109. const
  110.   HalfMax            = 16383; { MaxInt div 2 }
  111. type
  112.   DummyArray         = array[1..HalfMax] of Integer;
  113. var
  114.   A1                 : DummyArray absolute IAddr;
  115.   Indx,Temp          : Integer;
  116. begin
  117.   Temp := MaxInt;
  118.   Mndx := 0;
  119.   for Indx := 1 to Size do
  120.     if A1[Indx] < Temp then begin
  121.       Mndx := Indx;
  122.       Temp := A1[Indx]
  123.     end;
  124.   AMin := Temp
  125. end; { of func AMin }
  126.  
  127. function AMax(var IAddr; Size : Integer; var Mndx : Integer) : Integer;
  128. {
  129.        purpose       finds maximum value in integer array
  130.  
  131.        note          Size should be the size in *words*; if the
  132.                      the function SizeOf is used, then you need
  133.                      to divide the result by 2 before passing it on:
  134.                      MVal := AMax(IArray,(SizeOf(IArray) shr 1),Indx);
  135.  
  136.        last update   09 Jul 85
  137. }
  138. const
  139.   HalfMax            = 16383; { MaxInt div 2 }
  140. type
  141.   DummyArray         = array[1..HalfMax] of Integer;
  142. var
  143.   A1                 : DummyArray absolute IAddr;
  144.   Indx,Temp          : Integer;
  145. begin
  146.   Temp := - MaxInt - 1; { lowest possible integer value }
  147.   Mndx := 0;
  148.   for Indx := 1 to Size do
  149.     if A1[Indx] > Temp then begin
  150.       Mndx := Indx;
  151.       Temp := A1[Indx]
  152.     end;
  153.   AMax := Temp
  154. end; { of func AMax }
  155.  
  156.